package com.hkw.servlet;

import com.hkw.model.ClassRoom;
import com.hkw.model.Student;
import com.hkw.service.IClassRoomService;
import com.hkw.service.IStudentService;
import com.hkw.service.impl.ClassRoomServiceImpl;
import com.hkw.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 执行修改学生请求的servlet
 */
@WebServlet(name = "ReaUpdateServlet",urlPatterns = "/update")
public class ReaUpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置request编码格式
        String id = request.getParameter("id");
        String stname = request.getParameter("stname");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String remark = request.getParameter("remark");
        String classId = request.getParameter("classId");
        //生成student对象
        Student student = new Student(Integer.parseInt(id),stname,sex,birthday,email,remark,Integer.parseInt(classId));
        //实例接口服务
        IStudentService studentService = new StudentServiceImpl();
        //调用接口方法修改
        int i = studentService.updateStudent(student);
        if(i==1){
            request.setAttribute("updatesuccess","修改成功!");
            request.getRequestDispatcher("studentlist").forward(request,response);
        }else{
            request.setAttribute("updateerror","修改失败!");
            request.getRequestDispatcher("studentlist").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
